package com.snails.app.base.mvp.presenter;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.snails.app.base.mvp.model.AbstractModel;
import com.snails.app.base.mvp.view.AbstractView;

/**
 * @author lawrence
 * @date 2019-04-26 16:22
 */
public abstract class BasePresenter<V extends AbstractView, M extends AbstractModel>
        implements AbstractPresenter<V, M> {

    @Nullable protected V mView = null;       // 持有V层
    @Nullable protected M mModel = null;      // 持有M层

    public BasePresenter() {
        this.mModel = newModel();
        if (this.mModel != null) this.mModel.onAttachP(this);
    }

    @Override
    public void onAttachV(V view) {
        this.mView = view;
    }

    @Override
    public void onDetachV() {
        closeLoading();
        this.mView = null;
        if (this.mModel != null) this.mModel.onDetach();
    }

    @Override
    public void onCompleted(@Nullable Bundle bundle) {
        if (this.mView != null) mView.onCompleted(bundle);
    }
    @Override
    public void showLoading() {
        if (this.mView != null) this.mView.showLoading();
    }
    @Override
    public void showLoading(String message) {
        if (this.mView != null) this.mView.showLoading(message);
    }
    @Override
    public void closeLoading() {
        if (this.mView != null) this.mView.closeLoading();
    }
    @Override
    public void showToast(String message) {
        if (this.mView != null) this.mView.showToast(message);
    }
    @Override
    public void showToast(@StringRes int resId) {
        if (this.mView != null) this.mView.showToast(resId);
    }

}
